#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "DS1302.h"
#include "Key.h"
#include "Buzzer.h"
#include "Timer0.h"

extern char Clock_Time[] = {0,0,0};
unsigned char Buzzer;
unsigned char ClockSetFlashFlag,i;


/**
  * @brief  闪烁
  */
unsigned char Clock_SetFlash()
{
	ClockSetFlashFlag = !ClockSetFlashFlag;
	return ClockSetFlashFlag;
}


void Clock_Show()
{
		LCD_ShowString(1,1,"Clock:");
		LCD_ShowNum(1,8,Clock_Time[0],2);
		LCD_ShowString(1,10,":");
		LCD_ShowNum(1,11,Clock_Time[1],2);
}

void Clock_Ring(unsigned char KeyNum)
{
	if(Clock_Time[0]==DS1302_Time[3]&&Clock_Time[1]==DS1302_Time[4] && Clock_Time[2]==DS1302_Time[5])
	{
		Buzzer = 1;
	}
	//关闭闹钟
	if(KeyNum == 6 )
	{
		Buzzer = 0;
	}
	Buzzer_Time(Buzzer);
	
}

/**
  * @brief 闹钟设置 
  * @param  KeyNum按键值
  * @retval 无
  */
void Clock_Set(unsigned char KeyNum)
{
	unsigned char TimeSetSelect;
	if(KeyNum == 2)
	{
		TimeSetSelect++;
		TimeSetSelect%=2;
	}
	
	//前移动光标
	if(KeyNum == 3)
	{
		TimeSetSelect--;
		if(TimeSetSelect<0 ||TimeSetSelect>10)
		{
				TimeSetSelect = 1;
		}
	}
	i=i+1;
	i=0;
	LCD_ShowNum(2,10,TimeSetSelect,1);
	LCD_ShowString(2,10," ");
	//加
	if(KeyNum == 4)
	{
		Clock_Time[TimeSetSelect]++;
		if(Clock_Time[0]>23){Clock_Time[0]=0;}
		if(Clock_Time[1]>59){Clock_Time[1]=0;}
	}
		//按键3，减
	if(KeyNum == 5)
	{
		Clock_Time[TimeSetSelect]--;
		if(Clock_Time[0]<0){Clock_Time[0]=59;}
		if(Clock_Time[1]<0){Clock_Time[1]=59;}
	}
	
	
	//进行设置显示，让被选中的进行闪烁
	if(TimeSetSelect == 0 && ClockSetFlashFlag == 1){LCD_ShowString(1,8,"  ");}
	else{LCD_ShowNum(1,8,Clock_Time[0],2);}
	if(TimeSetSelect == 1 && ClockSetFlashFlag == 1){LCD_ShowString(1,11,"  ");}
	else{LCD_ShowNum(1,11,Clock_Time[1],2);}
}

